package com.hundsun.cprs.yyt.service.scheduler;

import com.hundsun.cprs.yyt.common.exception.ServiceCodeException;
import com.hundsun.cprs.yyt.service.system.service.ClearingTradeService;
import com.hundsun.cprs.yyt.service.system.service.TradeDayService;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.Date;

@Service("openTradeQuartzService")
public class OpenTradeQuartzService implements Serializable {

    Logger logger = Logger.getLogger(OpenTradeQuartzService.class);

    @Value("${clearing.trade.quartz.flag:true}")
    private String clearingTradeQuartzFlag;

    @Autowired
    private ClearingTradeService clearingTradeService;
    @Autowired
    private TradeDayService tradeDayService;

    public void openTrade(){
        try{
            //开关控制定时任务是否执行
            if (clearingTradeQuartzFlag.equals("true")){
                //判断当前时间是否为清算日，如果不是清算日则不进行后面的操作
                if (tradeDayService.isClearingDate(new Date())) {
                    if (logger.isInfoEnabled()) {
                        logger.info("对接清算中心，开市通知启动");
                    }

                    clearingTradeService.openTrade(new Date());

                    if (logger.isInfoEnabled()) {
                        logger.info("对接清算中心，开市通知完成");
                    }
                }else{
                    if (logger.isInfoEnabled()) {
                        logger.info("当前时间不是清算日，无需进行开市操作");
                    }
                }
            }else{
                if (logger.isDebugEnabled()) {
                    logger.info("定时任务配置已关闭，无需进行开市操作");
                }
            }
        }catch (ServiceCodeException e){
            logger.error("对接清算中心，开市通知启动失败：" + e);
        }catch (Exception e){
            logger.error("对接清算中心，开市通知启动失败：" + e);
        }
    }
}
